package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Wrapper;

@RestController //定义一个返回json数据的controller
@RequestMapping("/user")    //接口统一的路由
@Slf4j
public class UserController {

    @Autowired
    UserMapper userMapper;


    /**
     * 注册功能
     * @param user
     * @return
     */
    @PostMapping("/register")
    public Result<?> register(@RequestBody User user){
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername,user.getUsername());
        User selectUser = userMapper.selectOne(wrapper);
        if (selectUser != null){
            return Result.error("-1","用户已存在");
        }
        userMapper.insert(user);
        return Result.success();
    }

    /**
     * 登录功能
     * @param user
     * @return
     */

    @PostMapping("/login")
    public Result<?> login(@RequestBody User user){
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername,user.getUsername())
                .eq(User::getPassword,user.getPassword());
        User selectUser = userMapper.selectOne(wrapper);
        if (selectUser == null){
            //直接抛出异常
            return Result.error("-1","用户名或密码错误");
        }

        System.out.println(selectUser);
        return Result.success(selectUser);
    }

    /**
     * 新增用户
     * @return
     */
    @PostMapping
    public Result<?> save(@RequestBody User user){//@RequestBody将前台传过来的数据转换成对像    Result<?>  ?表示所有泛型

        //设置默认密码
        if(user.getPassword() == null){
            user.setPassword("123456");
        }

        userMapper.insert(user);
        return Result.success();
    }

    /**
     * 数据展示 ,这里使用的是分页查询，在分页查询之前需要在项目中添加MybatisPlus的分页插件
     * @param pageNum   当前页
     * @param pageSize  每页条数
     * @param search    模糊查询的关键字
     * @return
     */
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){
//        new Page<>(pageNum, pageSize);
        //条件查询，并分页
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(search),User::getUsername,search);

//        Page<User> userPage = userMapper.selectPage(new Page<>(pageNum, pageSize), Wrappers.<User>lambdaQuery().eq(User::getUsername, search));//模糊查询
        Page<User> userPage = userMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);

        return Result.success(userPage);
    }

    /**
     * 更新方法
     * @param user
     * @return
     */
    @PutMapping
    public Result<?> update(@RequestBody User user){//@RequestBody将前台传过来的数据转换成对像    Result<?>  ?表示所有泛型
//        System.out.println("更新："+user);
        userMapper.updateById(user);
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getId,user.getId());
        User selectOne = userMapper.selectOne(wrapper);
        System.out.println("测试，所获得修改后的user："+selectOne);
        return Result.success(selectOne);
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id){//@RequestBody将前台传过来的数据转换成对像    Result<?>  ?表示所有泛型
        userMapper.deleteById(id);
        return Result.success();
    }
}
